#coding=utf-8
'''
    参数控制界面
'''
import string

from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QWidget, QVBoxLayout, QFormLayout, QSlider, QLabel, QLineEdit, QFrame
from PySide2 import QtCore
from controller.params import Params

class DictEditor(QWidget):
    def __init__(self,rawDict:dict):
        super().__init__()
        self.rawDict=rawDict
        self.labelDict={}
        self.widgetDict={}
        self.valDict={}
        self.InitUI()
    def InitUI(self):
        '''
        初始化参数调试界面
        :return:
        '''
        self.formLayout=QFormLayout()
        for key,value in self.rawDict.items():
            self.labelDict[key] = QLabel()
            self.labelDict[key].setText(f"{key}:{value}")
            if type(value)==dict:
                '''
                子列表，使用递归做层次分布
                '''
                subEditor=DictEditor(value)
                self.valDict[key]=subEditor
                line = QFrame()
                line.setFrameShape(QFrame.HLine)
                line.setFrameShadow(QFrame.Sunken)
                self.formLayout.addRow(line)
                self.formLayout.addRow(key,subEditor)
            else:
                '''
                    是数值，建立内容
                '''
                self.valDict[key] = value
                if type(value)==list:
                    pass
                elif type(value)==str:
                    self.widgetDict[key] = QLineEdit()
                    self.widgetDict[key].setText(value)
                    self.widgetDict[key].textChanged.connect(lambda text, k=key: self.UpdateValue(k, text))
                    self.formLayout.addRow(self.labelDict[key],self.widgetDict[key])
                else:
                    self.widgetDict[key] = QSlider()
                    self.widgetDict[key].setOrientation(QtCore.Qt.Orientation.Horizontal)
                    self.widgetDict[key].setMinimum(0)
                    self.widgetDict[key].setStyleSheet('QSlider::handle:horizontal {'
                             '    background-color: #3498db;'
                             '    border: 1px solid #2980b9;'
                             '    width: 40px;'  # 设置滑块宽度
                             '    height: 20px;'  # 设置滑块高度
                             '    border-radius: 10px;'
                             '}'
                             'QSlider::groove:horizontal {'
                             '    background-color: #ecf0f1;'
                             '    border: 1px solid #bdc3c7;'
                             '    height: 20px;'  # 设置滑槽高度
                             '    border-radius: 10px;'
                             '}')
                    if type(value)==float:
                        self.widgetDict[key].setMaximum(int(value*300)+100)
                        self.widgetDict[key].setValue(int(value*100.0))
                        self.widgetDict[key].valueChanged.connect(lambda val,k=key: self.UpdateValue(k, val / 100))
                    elif type(value)==int or type(value)==bool:
                        self.widgetDict[key].setValue(value)
                        self.widgetDict[key].setMaximum(int(value * 3)+5)
                        self.widgetDict[key].valueChanged.connect(lambda val, k=key: self.UpdateValue(k, val))
                    self.formLayout.addRow(self.labelDict[key], self.widgetDict[key])
        self.setLayout(self.formLayout)
    def UpdateValue(self, key, value):
        self.valDict[key] = value
        if type(value)==float or type(value)==int:
            self.labelDict[key].setText(f"{key}: {value}")
        #self.ToDict()
    def ToDict(self):
        for key,val in self.valDict.items():
            if type(val)==DictEditor:
                self.rawDict[key]=val.ToDict()
            else:
                self.rawDict[key]=val
        return self.rawDict
class PPanel:
    '''
        参数控制器
    '''
    def __init__(self,rootObject=None):
        self.rootObject=rootObject
        self.ui_Main=QUiLoader().load("gui/qt_ui_design/params_ctrl.ui")
        self.paramDict={}
        self.editor=DictEditor(Params.controlParams)
        self.ui_Main.pLayout.addWidget(self.editor)
        self.ui_Main.saveBtn.clicked.connect(self.Save)
        self.ui_Main.syncBtn.clicked.connect(self.Sync)

    def Save(self):
        self.editor.ToDict()
        self.rootObject.DumpAdjConfig(self.editor.rawDict)

    def Sync(self):
        self.editor.ToDict()
        print(Params.controlParams)